import { Component, OnInit } from '@angular/core';
import {User} from "../../model/user";
import {Http} from "@angular/http";
import MTSConstants from "../../config/config";
import {CamSearchService} from "../../service/cam-search.service";
import {SingleOpenWinService} from "../../service/single-open-win.service";

@Component({
  selector: 'app-user-table',
  templateUrl: './user-table.component.html',
  styleUrls: ['./user-table.component.css']
})
export class UserTableComponent implements OnInit {

  //选择人员分页
  _current = 1;
  _pageSize = 10;
  _total = 1;
  _dataSet = [];
  queryId = "";
  queryName = "";

  //选择框
  _displayData: Array<any> = [];
  selected:User; //选中人员
  source:string="";  //判断开窗来源

  constructor(
    private http: Http,
    private singleOpenWinService: SingleOpenWinService
  ) { }

  ngOnInit() {
    this.getDatas("users");
  }

  refreshData(reset = false) {
    if (reset) {
      this._current = 1;
    }
    // this._allChecked = false;
    this.getDatas("users");
  };

  _displayDataChange($event) {
    this._displayData = $event;
  };

  getDatas(type){
    const url = `${MTSConstants.HOSTUser}openwin/${type}?pageNumber=${this._current}&pageSize=${this._pageSize}&queryId=${this.queryId}&queryName=${this.queryName}`;
    this.http.get(url)
      .toPromise()
      .then((res) => { return res.json()}).then(
      (res) => {
        this._dataSet = res.list;
        this._total = res.total;
        this._displayData = res.list;
      }
    )
  }

  search() {
    this.queryId = $('#sId').val() as string;
    this.queryName = $('#sName').val() as string;
    this.getDatas("users");
  }

  getUserInfo(data){
    this.selected = data;
    this.singleOpenWinService.valueChange.emit(this.source);
  }
}
